#include "zczh.h"
#include "ui_zczh.h"
#include <QMessageBox>
#include "dialoglogin.h"
#include "dlcw.h"

zczh::zczh(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::zczh)
{
    ui->setupUi(this);
    setWindowTitle("注册账号");
}

zczh::~zczh()
{
    delete ui;
}

void zczh::on_yesButton_clicked()
{
    Accp p1;
    p1 = (Accp)malloc(N);     //向内存申请一段空间
    int testAcc=1;
    std::string name=ui->username_zc->text().trimmed().toStdString();
    std::string password=ui->pswd_new->text().trimmed().toStdString();
    std::string password_cfm=ui->pswd_confirm->text().trimmed().toStdString();
    if(strlen(name.c_str())>10){
        QMessageBox::warning(this,"账号长度问题","请注册十位以下的账号");
        return;
    }
    else {
        QString str=ui->username_zc->text().trimmed();
        int nCount = str.count();
        for(int i = 0 ; i < nCount ; i++)
        {
            QChar cha = str.at(i);
            ushort uni = cha.unicode();
            if((uni >= 0x4E00 && uni <= 0x9FA5))
            {
                QMessageBox::warning(this,"err","账号只能由字母和数字构成");
                return;
            }
            if(!((uni>=65&&uni<=90)||(uni>=97&&uni<=122)||(uni>=48&&uni<=57)))
            {
                QMessageBox::warning(this,"err","账号只能由字母和数字构成");
                return;
            }
        }
    }
    if(strlen(password.c_str())<6||strlen(password.c_str())>10){
        QMessageBox::warning(this,"密码长度问题","请输入6-10位的密码");
        return;
    }

    if(strcmp(password.c_str(),password_cfm.c_str())!=0){
        QMessageBox::warning(this,"err","密码不一致，请重新输入");
         return;
    }
    else{
        Accp np, head;
        head = ff();
        np = head;
        std::string adminname;
        adminname="admin123";
        while (np != NULL)   //遍历链表，寻找当前输入的账户的账号
        {
            if (strcmp(np->name, name.c_str()) == 0||strcmp(adminname.c_str(),name.c_str())==0)
            {
                testAcc = 0;
                break;
            }
            np = np->next;
        }
        if (testAcc != 0)        //如果没有重复账号
        {
            strcpy(p1->name, name.c_str());
            strcpy(p1->password, password.c_str());
            save_account(p1,this);     //调用函数，将p1所指向的内容存储到文件中
            QMessageBox::information(this," ","注册成功");
            accept();
        }
        else{
            QMessageBox::warning(this,"err","账号已经存在");
        }
    }
}
